<?php

require ("system.class.php");

/*
 * 
 */

class DataBase {

    var $sys,
    $link,
    $fetchMode,
    $db_host,
    $db_user,
    $db_pass,
    $db_name,
    $con,
    $numResults,
    $result;

    function __construct($host, $user, $pass, $db) {
        $this->sys = new System();
        $this->db_host = $host;
        $this->db_user = $user;
        $this->db_pass = $pass;
        $this->db_name = $db;
        $this->result = array();
        $this->link = mysql_connect($host, $user, $pass);
        if (!$this->link)
            throw new Exception("Error: Can not conntect to MySQL database");
        $dbs = mysql_select_db($db, $this->link);
        if (!$dbs)
            throw new Exception("Error: Can not select MySQL database");
        $this->fetchMode = "obj";
        $this->con = true;
    }
    function getCount() {
        return $this->numResults;
    }

    function setFetchMode($mode) {
        $this->fetchMode = $mode;
    }
    
    function cleanResult() {
    	$this->result = NULL;
    }

    private function tableExists($table) {
        $tablesInDb = @mysql_query('SHOW TABLES FROM ' . $this->db_name . ' LIKE "' . $table . '"');
        if ($tablesInDb) {
            if (mysql_num_rows($tablesInDb) == 1) {
                return true;
            } else {
                return false;
            }
        }
    }

    public function select($table, $rows = '*', $where = null, $order = null) {
        $q = 'SELECT ' . $rows . ' FROM ' . $table;
        if ($where != null)
            $q .= ' WHERE ' . $where;
        if ($order != null)
            $q .= ' ORDER BY ' . $order;
        $this->sys->Debug($q);
        $query = @mysql_query($q);
        if ($query) {
            $this->numResults = mysql_num_rows($query);
            for ($i = 0; $i < $this->numResults; $i++) {
                $r = mysql_fetch_array($query);
                $key = array_keys($r);
                for ($x = 0; $x < count($key); $x++) {
                    if (!is_int($key[$x])) {
                        if (mysql_num_rows($query) > 1)
                            $this->result[$i][$key[$x]] = $r[$key[$x]];
                        else if (mysql_num_rows($query) < 1)
                            $this->result = null;
                        else
                            $this->result[$key[$x]] = $r[$key[$x]];
                    }
                }
            }
            return true;
        }
        else {
            throw new Exception("Error: Can not select rows from table");
        }
    }

    public function insert($table, $values, $rows = null) {
        if ($this->tableExists($table)) {
            $insert = 'INSERT INTO ' . $table;
            if ($rows != null) {
                $insert .= ' (' . $rows . ')';
            }
            for ($i = 0; $i < count($values); $i++) {
                if (is_string($values[$i]))
                    $values[$i] = '"' . $values[$i] . '"';
            }
            $values = implode(',', $values);
            $insert .= ' VALUES (' . $values . ')';
            $ins = @mysql_query($insert);

            if ($ins) {
                return true;
            } else {
                throw new Exception("Error: Can not insert into MySQL table");
            }
        }
    }

    public function delete($table, $where = null) {
        if ($this->tableExists($table)) {
            if ($where == null) {
                $delete = 'DELETE ' . $table;
            } else {
                $delete = 'DELETE FROM ' . $table . ' WHERE ' . $where;
            }
            $del = @mysql_query($delete);

            if ($del) {
                return true;
            } else {
                throw new Exception("Error: Can not delete from MySQL table");
            }
        } else {
            throw new Exception("Error: Can not delete from MySQL table");
        }
    }

    public function update($table, $rows, $where) {
        if ($this->tableExists($table)) {
            for ($i = 0; $i < count($where); $i++) {
                if ($i % 2 != 0) {
                    if (is_string($where[$i])) {
                        if (($i + 1) != null)
                            $where[$i] = '"' . $where[$i] . '" AND ';
                        else
                            $where[$i] = '"' . $where[$i] . '"';
                    }
                }
            }
            $where = implode('', $where);


            $update = 'UPDATE ' . $table . ' SET ';
            $keys = array_keys($rows);
            for ($i = 0; $i < count($rows); $i++) {
                if (is_string($rows[$keys[$i]])) {
                    $update .= $keys[$i] . '="' . $rows[$keys[$i]] . '"';
                } else {
                    $update .= $keys[$i] . '=' . $rows[$keys[$i]];
                }

                if ($i != count($rows) - 1) {
                    $update .= ',';
                }
            }
            $update .= ' WHERE ' . $where;
            $query = @mysql_query($update);
            if ($query) {
                return true;
            } else {
                throw new Exception("Error: Can not update rows in MySQL table");
            }
        } else {
            throw new Exception("Error: MySQL table does not exists");
        }
    }

    public function getResult() {
        return $this->result;
    }

    function query($query) {
        $this->sys->debug($query);
        return mysql_query($query);
    }

    function fetch($query) {
        if ($this->fetchMode == "obj")
            return mysql_fetch_object($query);
        else
            return mysql_fetch_array($query);
    }

}

?>